//
//  UIImage+STImage.m
//  OCVVeibo
//
//  Created by leergou on 16/8/2.
//  Copyright © 2016年 WhiteHouse. All rights reserved.
//

#import "UIImage+STImage.h"

@implementation UIImage (STImage)

// 获取屏幕截图
+ (UIImage *)getScreenShot{
    
    // 得到windows
    UIWindow *window = STApplication.keyWindow;
    
    // 开启图片上下文
//    UIGraphicsBeginImageContext(window.bounds.size);
    UIGraphicsBeginImageContextWithOptions(window.bounds.size, NO, 0);
    // 将窗口渲染到图片上下文
    [window drawViewHierarchyInRect:window.bounds afterScreenUpdates:NO];
    // 从上下文中获取图片
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    // 关闭图形上下文
    UIGraphicsEndImageContext();
    
    return image;
}

// 对图片裁圆角
- (UIImage *)imageWithCornerRadius:(CGFloat)radius {
    CGRect rect = (CGRect){0.f, 0.f, self.size};
    
    UIGraphicsBeginImageContextWithOptions(self.size, NO, UIScreen.mainScreen.scale);
    CGContextAddPath(UIGraphicsGetCurrentContext(),
                     [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius].CGPath);
    CGContextClip(UIGraphicsGetCurrentContext());
    
    [self drawInRect:rect]; // 裁剪功能的核心
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return image;
}

/*
 
  利用图片上下文,进行裁剪
 */

@end
